package com.dmxtechnologies.community.war;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import com.dmxtechnologies.community.ejb.util.Base64Util;
import com.dmxtechnologies.community.pojo.CookieModel;

@Named("cmsLastViewCookieAccessBean")
@RequestScoped
public class CMSLastViewCookieAccessBean extends CMSBaseAccessBean {
	
	private List<CookieModel> cookieModels;
	
	public List<CookieModel> getCookieModels() {
		return cookieModels;
	}

	public void setCookieModels(List<CookieModel> cookieModels) {
		this.cookieModels = cookieModels;
	}

	@PostConstruct
	public void getCookieModel(){
		cookieModels = new ArrayList<>();
		//去掉重复的浏览记录
		List<String> viewList = new ArrayList<>();
		request = (HttpServletRequest) FacesContext.getCurrentInstance()
				.getExternalContext().getRequest();
		cookies = request.getCookies();
		if(cookies != null){
			for (Cookie cookie : cookies) {
				if (cookie.getName().equals("lastView")) {
					controll = true;
					String value = Base64Util.getBase64String(cookie.getValue());
					String[] vals = value.split(" ");
					for (int i=vals.length-1;i>=0;i--) {
						CookieModel cookieModel = new CookieModel();
						String[] values = vals[i].split(",");
						cookieModel.setTitleName(values[0]);
						cookieModel.setUrl(values[1]);
						cookieModel.setPicUrl(values[2]);
						if(!viewList.contains(values[1])){
							viewList.add(values[1]);
							cookieModels.add(cookieModel);
						}
					}
					break;
				}
			}
		}
		
	   //浏览记录初始化
		if(cookieModels.size() < 3){
			//写三个不一样的数据 初始化 页面
			CookieModel cookieModel1 = new CookieModel();
			cookieModel1.setTitleName("平安社区");
			cookieModel1.setUrl("1");
			cookieModel1.setPicUrl("http://192.168.1.104:8076/cms-portal/res/poster/category/67683fba-3daf-49a4-aea7-f93b42d0b571.png");
			cookieModels.add(cookieModel1);
			
			CookieModel cookieModel2 = new CookieModel();
			cookieModel2.setTitleName("民生信息");
			cookieModel2.setUrl("2");
			cookieModel2.setPicUrl("http://192.168.1.104:8076/cms-portal/res/poster/category/6fc7e671-31b7-40fc-9197-3e2b65c5a6b0.png");
			cookieModels.add(cookieModel2);
			
			CookieModel cookieModel3 = new CookieModel();
			cookieModel3.setTitleName("公共信息");
			cookieModel3.setUrl("3");
			cookieModel3.setPicUrl("http://192.168.1.104:8076/cms-portal/res/poster/category/4a41ed70-b9d1-4cb7-a129-549ba3d2c615.png");
			cookieModels.add(cookieModel3);
			
		}
	
	}
}
